import json

import requests
from lxml import etree
import re
import os

response = requests.get('https://book.zongheng.com/store/c0/c0/b0/u4/p1/v0/s9/t0/u0/i1/ALL.html')
# print(response.content.decode())
url = response.content.decode()
result = etree.HTML(url)
book_list = result.xpath("//div[@class='bookinfo']")
for book in book_list:
    title = book.xpath('.//div[@class="bookname"]/a/text()')[0]
    book_url = book.xpath('.//div[@class="bookname"]/a/@href')[0]
    print(title, book_url)
    os.makedirs(f'./纵横小说/{title}', exist_ok=True)
    print(book_url)
    book_id = re.search(r'\d+', book_url).group()
    print(book_id)
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
        'Cookie': "ZHID=CFE08C1952C1E7B838EF99BCAE67E20D; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%221916a8079a924b-0548ea69fc4306-26001e51-2073600-1916a8079aa9cb%22%2C%22%24device_id%22%3A%221916a8079a924b-0548ea69fc4306-26001e51-2073600-1916a8079aa9cb%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%7D; zhffr=0; ver=2018; zh_visitTime=1729688436324; Hm_lvt_c202865d524849216eea846069349eb9=1729688436; HMACCOUNT=AFD53293F7A60F46;"
    }
    book_detail_response = requests.post('https://bookapi.zongheng.com/api/chapter/getChapterList',
                                         data={'bookId': f'{book_id}'}, headers=header)
    print(book_detail_response.content.decode())
    book_detail = json.loads(book_detail_response.content.decode())
    for i in book_detail['result']['chapterList']:
        for chapter in i['chapterViewList']:
            print(chapter['chapterName'], chapter['chapterId'])
            detail_chapter = requests.get(f'https://read.zongheng.com/chapter/{book_id}/{chapter["chapterId"]}.html')
            print(detail_chapter.content.decode())
            root = etree.HTML(detail_chapter.content.decode())
            chapter_detail = root.xpath('//div[@class="content"]/p/text()')
            content = "\n".join(chapter_detail)
            with open(f'./纵横小说/{title}/{chapter["chapterName"]}.txt', 'a', encoding='utf-8') as f:
                f.write(content)